
<template>
  <el-cascader ref="cascader" :value="propsValue" :props="cascader_props" @change="cascaderChange"
    clearable></el-cascader>
</template>

<script>
export default {
  name: "pack_material_cascader",
  props: {
    typeId: {
      type: String,
      default: null,
    },
    packId: {
      type: String,
      default: null,
    },

  },
  data() {
    return {
      value: [],
      cascader_props: {
        lazy: true,
        value: "id",
        label: "label",
        lazyLoad: (node, resolve) => {
          const { level, data } = node;
          if (level == 0) {
            this.$ajax({
              url: "/wms-api/base/WmsBasePackMaterialTypeRpc/selectLabel.json",
              method: "post",
              data: { warehouseId: this.$loginUser.currentWarehouseId },
              success: (res) => {
                let list = res.data.rows.map(it => {
                  it.label = it.name
                  it.code = it.code
                  it.leaf = false
                  return it
                });
                resolve(list);
              },
              failure: () => {
                resolve([]);
              },
            });
          } else {
            this.$ajax({
              url: "/wms-api/base/WmsBasePackMaterialStockRpc/selectBasePackMaterialLabel.json",
              method: "post",
              data: {
                warehouseId: this.$loginUser.currentWarehouseId,
                typeId: data.id,
              },
              success: (res) => {
                let list = res.data.rows.map(it => {
                  it.label = it.name
                  it.code = it.code
                  it.leaf = true
                  return it
                });
                resolve(list);
              },
              failure: () => {
                resolve([]);
              },
            });
          }
        },
      },
    };
  },
  computed: {
    propsValue() {
      // if (this.categId0) out.push(this.categId0);
      // if (this.categId1) out.push(this.categId1);
      // if (this.categId2) out.push(this.categId2);
      if (this.typeId && this.packId) return [this.typeId, this.packId];
      return [];
    },
  },
  methods: {
    cascaderChange(e) {
      this.$emit("input", e);
      this.$emit("change", e);


      if (this.$refs.cascader) {
        const node = this.$refs.cascader.getCheckedNodes();
        if (_.isEmpty(node)) return
        const nodePath = _.head(node).pathNodes;

        if (nodePath && nodePath.length > 1) {
          this.$emit(
            "update:typeId", nodePath[0].data.id
          );
          this.$emit(
            "update:typeName", nodePath[0].data.label
          );

          this.$emit(
            "update:typeCode", nodePath[0].data.code
          );
          this.$emit(
            "update:packId", nodePath[1].data.id
          );

          this.$emit(
            "update:packName", nodePath[1].data.label
          );

          this.$emit(
            "update:packCode", nodePath[1].data.code
          );
          this.$emit("changeItem", nodePath[1].data);
          return
        }
      }
      this.$emit(
        "update:typeId", null
      );
      this.$emit(
        "update:typeName", null
      );

      this.$emit(
        "update:typeCode", null
      );
      this.$emit(
        "update:packId", null
      );

      this.$emit(
        "update:packName", null
      );

      this.$emit(
        "update:packCode", null
      );
      this.$emit("changeItem", null);
    },
  },
};
</script>
